package com.xuelang.pipeline.worker.app.deployment.impl;

import com.xuelang.pipeline.worker.app.common.util.K8sUtils;
import com.xuelang.pipeline.worker.app.deployment.DeploymentService;
import com.xuelang.pipeline.worker.client.deployment.dto.DeployQueryDTO;
import io.kubernetes.client.custom.V1Patch;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.apis.AppsV1Api;
import io.kubernetes.client.openapi.models.V1Deployment;
import io.kubernetes.client.openapi.models.V1DeploymentList;
import io.kubernetes.client.openapi.models.V1Scale;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liuyuan.ly
 * @since 2024/8/8 16:36
 */
@Service
@Slf4j
public class DeploymentServiceImpl implements DeploymentService {

    @Override
    public V1DeploymentList getDeploymentList(DeployQueryDTO queryDTO) {
        K8sUtils.initApiClient(queryDTO.getKubeConfig());
        AppsV1Api api = new AppsV1Api();
        V1DeploymentList v1DeploymentList;
        try {
            v1DeploymentList = api.listNamespacedDeployment(queryDTO.getNamespaceName(), null, null,
                    null, null, queryDTO.getLabelName(), null, null, null,
                    null, null);
        } catch (ApiException e) {
            log.info("获取deployment列表信息失败:{},堆栈信息:", e.getResponseBody(), e);
            throw new RuntimeException(e.getResponseBody());
        }

        return v1DeploymentList;
    }

    @Override
    public V1Scale patchDeployment(DeployQueryDTO queryDTO) {
        K8sUtils.initApiClient(queryDTO.getKubeConfig());
        AppsV1Api api = new AppsV1Api();
        V1Patch body = new V1Patch(queryDTO.getBody());
        V1Scale v1Scale;
        try {
            v1Scale = api.patchNamespacedDeploymentScale(queryDTO.getDeploymentName(), queryDTO.getNamespaceName(), body, null, null, null, null, true);
        } catch (ApiException e) {
            log.info("替换deployment失败:{},堆栈信息:", e.getMessage(), e);
            throw new RuntimeException(e.getMessage());
        }
        return v1Scale;
    }

    @Override
    public List<String> listAvailableDeploymentName(DeployQueryDTO queryDTO) {
        K8sUtils.initApiClient(queryDTO.getKubeConfig());
        AppsV1Api api = new AppsV1Api();
        List<String> result = new ArrayList<>();
        try {
            String labelSelector = "app.oam.dev/component=" + queryDTO.getLabelName(); // 替换为实际标签选择器
            V1DeploymentList deploymentList = api.listNamespacedDeployment(
                    queryDTO.getNamespaceName(),
                    null,
                    null,
                    null,
                    null,
                    labelSelector,
                    null,
                    null,
                    null,
                    null,
                    null
            );

            log.info("获取deployment列表信息:{}", deploymentList);

            for (V1Deployment deployment : deploymentList.getItems()) {
                if (deployment != null && deployment.getStatus() != null) {
                    Integer availableReplicas = deployment.getStatus().getAvailableReplicas();
                    if (availableReplicas != null && availableReplicas > 0 && deployment.getMetadata() != null) {
                        result.add(deployment.getMetadata().getName());
                    }
                }
            }

        } catch (ApiException e) {
            log.info("listAvailableDeployment失败:{},堆栈信息:", e.getMessage(), e);
            throw new RuntimeException(e.getMessage());
        }
        return result;
    }
}
